package com.llkj.qingnianguoxue.home;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.HotCourseAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.bean.BaseCourseBean;
import com.llkj.qingnianguoxue.bean.CourseBean;
import com.llkj.qingnianguoxue.course.CourseDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 热门课程
 */
public class HotCourseFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.refresh_layout)
    protected SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    protected RecyclerView lvCourse;
    private HotCourseAdapter gvCourseAdapter;
    private List<CourseBean> mCourseData;
    private int totalSize, mPage;


    /**
     *
     */
    public static HotCourseFragment newInstance(String param1) {
        HotCourseFragment fragment = new HotCourseFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_hot_course, container, false);

        ButterKnife.bind(this,rootView);

        initView();
        initListener();

        onRefresh();
        return rootView;
    }

    private void initView(){
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        int width = DisplayUtil.dip2px(getActivity(),10);
        lvCourse.setPadding(width,width,width,width);
        GridLayoutManager mgr = new GridLayoutManager(getActivity(), 2);
        lvCourse.setLayoutManager(mgr);
        lvCourse.addItemDecoration(new GridSpacingItemDecoration(2, 10, false));
        mCourseData = new ArrayList<>();
        gvCourseAdapter = new HotCourseAdapter(R.layout.item_jing_xuan_action,mCourseData);
        gvCourseAdapter.setType(1);
        lvCourse.setAdapter(gvCourseAdapter);
    }

    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        gvCourseAdapter.setOnLoadMoreListener(this);
        lvCourse.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                CourseBean item = mCourseData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("id",item.getId());
                startActivity(CourseDetailActivity.class,bundle);
            }
        });
    }

    @Override
    public void onLoadMore() {

        mPage++;
        getCourse();
    }

    @Override
    public void onRefresh() {
        mPage=1;
        getCourse();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, gvCourseAdapter, null, lvCourse, totalSize);
    }

    private void getCourse() {
        HttpMethod.hotCurriculum(getActivity(), this, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_INDEX_HOT_CURRICULUM) {
            BaseCourseBean bean = GsonUtil.GsonToBean(data, BaseCourseBean.class);
            if (bean != null) {
                totalSize = Integer.parseInt(bean.getNum());

                if (mPage == 1) {
                    mCourseData.clear();
                    LoadDataHelper.refreshComplete(gvCourseAdapter, mCourseData, refreshLayout, lvCourse);
                } else {
                    gvCourseAdapter.setEnableLoadMore(true);
                    gvCourseAdapter.loadMoreComplete();
                }
                mCourseData.addAll(bean.getCurriculum());
                gvCourseAdapter.notifyDataSetChanged();
            }
        }
    }
}
